package com.kailong.dao.student;

import com.kailong.dao.BaseDao;
import com.kailong.pojo.Seat;
import com.kailong.pojo.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectStudentDao {
    public static  int select(Student student){
        String sql = "select password from student where sno = ?";
        Object [] params = {
                student.getSno(),
        };
        return BaseDao.exectuIUD(sql,params);
    }
    public static int selectBySno(String sno,String pwd){
        int count = 0;
        Student student = null;
        //声明结果集
        ResultSet rs = null;
        //获取连接对象
        Connection conn = BaseDao.getconn();

        PreparedStatement ps = null;
        try{
            String sql = "select count(*) from student where sno = ? and spassword = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,sno);
            ps.setString(2,pwd);
            rs = ps.executeQuery();
            while (rs.next()){
               count = rs.getInt(1);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeall(rs,ps,conn);
        }


        return  count;
    }
    public static Student selectAdmin(String sno,String pwd){
        Student student = null;
        ResultSet rs = null;
        Connection conn = BaseDao.getconn();
        PreparedStatement ps = null;
        try{
            String sql = "select * from student where sno = ? and spassword = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,sno);
            ps.setString(2,pwd);
            rs = ps.executeQuery();
            while(rs.next()){
                student = new Student(
                        rs.getString("sno"),
                        rs.getString("sname"),
                        rs.getString("ssex"),
                        rs.getString("sphoneNumber"),
                        rs.getString("sclass"),
                        rs.getString("smajor"),
                        rs.getString("scollege"),
                        rs.getString("spassword")
                );
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  student;
    }
    public static int deleteStudent(String sno){
        int count = 0;
        ResultSet rs = null;
        Connection conn = BaseDao.getconn();
        PreparedStatement ps = null;
        try{
            String sql = "delete from student where sno = ?";
            ps= conn.prepareStatement(sql);
            ps.setString(1,sno);
            count  = ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            BaseDao.closeall(null,ps,conn);
        }
        return count;
    }
}
